﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraTreeList.Columns;
using ProgNetComponentsDx.Language;
using ProgNetComponentsDx.Enums;

namespace ProgNetComponentsDx.Browsers
{
    public static class BrowserTools
    {
        public static void PrepareProperties(RepositoryItemCheckEdit rep, BrowserColumn col)
        {
            if (rep != null && col != null)
            {
                rep.NullText = "";
                rep.AllowGrayed = true;
                if (col.ValueChecked.IsNotNull())
                    rep.ValueChecked = col.ValueChecked;
                if (col.ValueUnchecked.IsNotNull())
                    rep.ValueUnchecked = col.ValueUnchecked;
                rep.ValueGrayed = null;
            }
        }
        public static void PrepareProperties(RepositoryItemDateEdit rep, BrowserColumn col)
        {
            if (rep != null && col != null)
            {
                rep.NullText = "";
            }
        }
        public static void PrepareProperties(RepositoryItemLookUpEdit rep, BrowserColumn col)
        {
            if (rep != null && col != null)
            {
                rep.NullText = "";
            }
        }
        /*public static void PrepareProperties(RepositoryItemGridLookUpEdit rep, BrowserColumn col)
        {
            if (rep != null && col != null)
            {
                rep.NullText = "";
                rep.CharacterCasing = col.CharacterCasing;
            }
        }
         * */
        public static void PrepareProperties(RepositoryItemCalcEdit rep, BrowserColumn col)
        {
            if (rep != null && col != null)
            {
                rep.NullText = "";
            }
        }
        public static void PrepareProperties(RepositoryItemSpinEdit rep, BrowserColumn col)
        {
            if (rep != null && col != null)
            {
                rep.NullText = "";
            }
        }
        public static void PrepareProperties(RepositoryItemTextEdit rep, BrowserColumn col)
        {
            if (rep != null && col != null)
            {
                rep.NullText = "";
            }
        }
        public static GridColumn CreateColumn(BrowserColumn col, GridView gvMain, BrowserProvider dataSourceProvider1)
        {
            GridColumn c = null;
            if (col != null)
            {
                c = new GridColumn();
                c.FieldName = col.Name.ToUpper();
                c.Width = col.Width;
                if (col.Visible)
                {
                    c.Visible = true;
                    c.VisibleIndex = gvMain.Columns.Count + 1;
                }
                else
                {
                    c.VisibleIndex = -1;
                    c.OptionsColumn.ShowInCustomizationForm = false;
                }
                c.OptionsColumn.AllowShowHide = false;
                string caption = col.Caption;
                if (caption.IsNull() && c.Visible)
                {
                    c.Caption = ProgNetLanguage.sGetStringDefault(col.Name, col.Name);
                }
                else
                    c.Caption = caption;
                RepositoryItem repItem = null;
                switch (col.ColumnType)
                {
                    case BrowserColumnType.CalcEdit:
                        repItem = new RepositoryItemCalcEdit();
                        repItem.Name = "Rep" + col.Name;
                        PrepareProperties(repItem as RepositoryItemCalcEdit, col);
                        break;
                    case BrowserColumnType.CheckEdit:
                        repItem = new RepositoryItemCheckEdit();
                        repItem.Name = "Rep" + col.Name;
                        PrepareProperties(repItem as RepositoryItemCheckEdit, col);
                        break;
                    case BrowserColumnType.DateEdit:
                    case BrowserColumnType.DateTimeEdit:
                        repItem = new RepositoryItemDateEdit();
                        ((RepositoryItemDateEdit)repItem).Mask.EditMask = "d";
                        if(col.ColumnType == BrowserColumnType.DateTimeEdit)
                            ((RepositoryItemDateEdit)repItem).Mask.EditMask = "g";
                        ((RepositoryItemDateEdit)repItem).Mask.UseMaskAsDisplayFormat = true;

                        repItem.Name = "Rep" + col.Name;
                        PrepareProperties(repItem as RepositoryItemDateEdit, col);
                        break;
                    case BrowserColumnType.LookUpEdit:
                        if (dataSourceProvider1 == null)
                            dataSourceProvider1 = new BrowserProvider(null);

                        repItem = new RepositoryItemLookUpEdit();
                        repItem.Name = "Rep" + col.Name;
                        PrepareProperties(repItem as RepositoryItemLookUpEdit, col);
                        if (col.BrowserId.IsNotNull())
                            dataSourceProvider1.GetSettings(repItem).BrowserId = col.BrowserId;
                        break;
                    /*case BrowserColumnType.GridLookUpEdit:
                        if (dataSourceProvider1 == null)
                            dataSourceProvider1 = new DataProvider();

                        repItem = new RepositoryItemGridLookUpEdit();
                        repItem.Name = "Rep" + col.Name;
                        PrepareProperties(repItem as RepositoryItemGridLookUpEdit, col);
                        if (StringExt.IsNotNull(col.BrowserName))
                            dataSourceProvider1.SetBrowserName(repItem, col.BrowserName);
                        if (col.Browser != null)
                            dataSourceProvider1.SetBrowser(repItem, col.Browser);
                        break;*/
                    case BrowserColumnType.SpinEdit:
                        repItem = new RepositoryItemSpinEdit();
                        repItem.Name = "Rep" + col.Name;
                        PrepareProperties(repItem as RepositoryItemSpinEdit, col);
                        break;
                    case BrowserColumnType.TextEdit:
                        repItem = new RepositoryItemTextEdit();
                        repItem.Name = "Rep" + col.Name;
                        PrepareProperties(repItem as RepositoryItemTextEdit, col);
                        break;
                }
                if (!gvMain.GridControl.RepositoryItems.Contains(repItem))
                    gvMain.GridControl.RepositoryItems.Add(repItem);
                c.ColumnEdit = repItem;
            }
            return c;
        }
        public static void DisableGridCustomize(GridView gvMain)
        {
            gvMain.OptionsBehavior.Editable = false;
            gvMain.OptionsCustomization.AllowFilter = false;
            gvMain.OptionsCustomization.AllowGroup = false;
            gvMain.OptionsCustomization.AllowColumnMoving = false;
            gvMain.OptionsMenu.EnableColumnMenu = false;
            gvMain.OptionsMenu.EnableFooterMenu = false;
            gvMain.OptionsMenu.EnableGroupPanelMenu = false;
            gvMain.OptionsSelection.EnableAppearanceFocusedCell = false;
            gvMain.OptionsView.ShowFilterPanelMode = DevExpress.XtraGrid.Views.Base.ShowFilterPanelMode.Never;
            gvMain.OptionsView.ShowGroupPanel = false;
            AppearanceObjectExt.SetReadOnlyAppearance(gvMain.Appearance.Row);
        }

        public static LookUpColumnInfo CreateColumn(BrowserColumn col)
        {
            LookUpColumnInfo c = null;
            if (col != null)
            {
                c = new LookUpColumnInfo(col.Name.ToUpper(), col.Width);
                string caption = col.Caption;
                if (caption.IsNull())
                    caption = ProgNetLanguage.sGetStringDefault(col.Name, col.Name);
                c.Caption = caption;
                c.Visible = col.Visible;
            }
            return c;
        }

        public static TreeListColumn CreateColumn(BrowserColumn col, DevExpress.XtraTreeList.TreeList treeList, BrowserProvider dataProvider)
        {
            TreeListColumn c = null;
            if (col != null)
            {
                c = treeList.Columns.Add();
                string caption = col.Caption;
                if (caption.IsNull())
                    caption = ProgNetLanguage.sGetStringDefault(col.Name, col.Name);
                c.FieldName = col.Name.ToUpper();
                c.Width = col.Width;
                c.Caption = caption;
                c.Visible = col.Visible;
            }
            return c;
        }
    }
}
